//------------------------------------------------------------------------------
#include <QtGui>
#include <QMessageBox>
#include <QDate>
#include <QDesktopServices>
#include <QSqlRecord>
#include "CFindFrm.h"
#include "CSelectTypeFactureFrm.h"
#include "CBLsNonFactureFrm.h"
#include "CComboResultFrm.h"
#include "CTextEditDelegate.h"
#include "CSelectPlageFactureFrm.h"
#include "CPrintFrm.h"
#include "CSelectArticleFrm.h"
#include "CFacturesFrm.h"
#include "version.h"
#include "CMySQL.h"
#include "CHelp.h"
#include "CRequest.h"
#include "CReportRequestData.h"
//------------------------------------------------------------------------------
CFacturesFrm::CFacturesFrm(QString name, QString lastCode, QWidget *parent) : CMyMainWindow(name, parent)
{
	setActionCur(CGlobal::acConstruct);
	
	setupUi(this);

	setStatusBar(NULL);
	
	actLock->setChecked(true);
	
	masterTable="teteFactures";
	primaryKey="code";
	curCode="0";

	tabWidget->removeTab(0);
	createGrille();
	
	document->addItem("Avoir", QVariant("A"));
	document->addItem("Facture", QVariant("F"));
	
	commentaires=new CTextWidget(this);
	commentaires->setChamp("commentaires");
	commentaires->setTable("teteFactures");
	connect(commentaires, SIGNAL(textChanged()), this, SLOT(onChange()));
	connect(commentaires, SIGNAL(invokeHelp(CChamp*)), this, SLOT(onInvokeHelp(CChamp*)));
	tabWidget->addTab(commentaires, QIcon(), tr("Commentaires"));
	
	loadSchema();
	
	loadPrintPopupMenu(toolBar);

	if(CMySQL::getInstance()->getNbEnregistrement(masterTable) == 0)
	{
		setActionCur(CGlobal::acTableVide);
	}else
	{
		if(!chargeDonnees(lastCode, CGlobal::seNone)) chargeDonnees("", CGlobal::sePremier);
	}
}
//------------------------------------------------------------------------------
void CFacturesFrm::setNumero(int numero)
{
	chargeDonnees(QString::number(numero), CGlobal::seNone);	
}
//------------------------------------------------------------------------------
void CFacturesFrm::setActionCur(CGlobal::EAction action)
{
	actionCur=action;
	
	switch(actionCur)
	{
		case CGlobal::acNone:
			actPremiereFiche->setEnabled(true);
			actFichePrecedente->setEnabled(true);
			actFicheSuivante->setEnabled(true);
			actDerniereFiche->setEnabled(true);
			
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(true);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			
			actBLsNonFacture->setEnabled(true);
			
			actRechercheUneFiche->setEnabled(true);
			enableBtnPrint(true);
			actPrintMultipage->setEnabled(true);
			
			enableChamps(true);
			document->setEnabled(true);
			tabWidget->setEnabled(true);
			cmdAdresse->setEnabled(true);
			
			avertModifClientShown=false;
			break;
		case CGlobal::acTableVide:
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			
			actBLsNonFacture->setEnabled(true);
			
			actRechercheUneFiche->setEnabled(false);
			enableBtnPrint(false);
			actPrintMultipage->setEnabled(false);
			
			clearChamps();
			enableChamps(false);
		
			code->setValue("");
			creation->setValue("");
			devis->setValue("");
			devis->setData(QVariant());
			commande->setValue("");
			commande->setData(QVariant());
			echeance->setValue("");
			
			document->setEnabled(false);
			
			tabWidget->setEnabled(false);
			lignesWidget->setRowCount(0);
			cmdAdresse->setEnabled(false);
			break;
		case CGlobal::acAjout:
		case CGlobal::acAjoutFromDevis:
		case CGlobal::acAjoutFromCommande:
		case CGlobal::acAjoutFromClient:
			clearChamps();
			initChamps();
			enableChamps(true);
			document->setEnabled(true);
	
			code->setText("");
			codeClient->setFocus();
			lignesWidget->setRowCount(0);
			
			document->setCurrentIndex(1);
			
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(false);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(true);
			actAnnuler->setEnabled(true);
			
			actBLsNonFacture->setEnabled(false);
			
			actRechercheUneFiche->setEnabled(false);
			enableBtnPrint(false);
			actPrintMultipage->setEnabled(false);
			
			tabWidget->setEnabled(false);
			cmdAdresse->setEnabled(false);
			break;
		case CGlobal::acModif:
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(false);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(true);
			actAnnuler->setEnabled(true);
			
			actBLsNonFacture->setEnabled(false);
			
			actRechercheUneFiche->setEnabled(false);
			enableBtnPrint(false);
			actPrintMultipage->setEnabled(false);
			
			tabWidget->setEnabled(true);
			cmdAdresse->setEnabled(false);
			break;
		case CGlobal::acBloque:
			actPremiereFiche->setEnabled(true);
			actFichePrecedente->setEnabled(true);
			actFicheSuivante->setEnabled(true);
			actDerniereFiche->setEnabled(true);
			
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			
			actBLsNonFacture->setEnabled(true);
			
			actRechercheUneFiche->setEnabled(true);
			enableBtnPrint(true);
			actPrintMultipage->setEnabled(true);
			
			enableChamps(false);
			document->setEnabled(false);
			tabWidget->setEnabled(false);
			cmdAdresse->setEnabled(false);
			break;
		case CGlobal::acAfficheLigne:
		default:
			break;
	}
}
//------------------------------------------------------------------------------
CGlobal::EAction CFacturesFrm::getActionCur(void)
{
	return actionCur;
}
//------------------------------------------------------------------------------
void CFacturesFrm::closeEvent(QCloseEvent * event)
{
	if(getActionCur() != CGlobal::acNone && getActionCur() != CGlobal::acTableVide && getActionCur() != CGlobal::acBloque)
	{
		QMessageBox msgBox(	QMessageBox::Warning, tr("Attention"), 
							tr("Veuillez annuler ou valider avant de sortir"), QMessageBox::Ok);
		msgBox.exec();	
		
		event->ignore();
	}else
	{
		emit quit(objectName(), curCode);
		event->accept();
	}
}
//------------------------------------------------------------------------------
void CFacturesFrm::createGrille(void)
{
	CTextEditDelegate *delegate=new CTextEditDelegate();
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelString(cnfBl, "nomBL", tr("BL"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelInt(cnfLigne, "numeroLigne", tr("Ligne"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnfArticle, "article", tr("Article"), Qt::AlignLeft, 
			new QIcon(":coMexpert/images/camera.png"), Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnfDescArticle, "descriptionArticle", tr("Description")));
	columnModel->append(new CColumnModelDouble(cnfQuantite, "quantite", tr("Qté.")));
	columnModel->append(new CColumnModelString(cnfUnite, "unite", tr("Unité"), Qt::AlignLeft, 
			new QIcon(":coMexpert/images/attach.png"), Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDouble(cnfPrix, "prixUnitaireHT", tr("Prix")));
	columnModel->append(new CColumnModelDouble(cnfTotal, "", tr("Total"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDouble(cnfTva, "tauxTva", tr("Taux de TVA"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	
	lignesWidget=new CGrilleWidget(columnModel, this);
	connect(lignesWidget, SIGNAL(ajoute(bool&)), this, SLOT(ajouteLigne(bool&)));
	connect(lignesWidget, SIGNAL(supprime(int, bool&)), this, SLOT(supprimeLigne(int, bool&)));
	connect(lignesWidget, SIGNAL(cellChanged(int, int)), this, SLOT(modifiLigne(int, int)));
	connect(lignesWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(ligneDoubleClicked(int, int)));
	tabWidget->addTab(lignesWidget, tr("Lignes"));

	lignesWidget->setColumnWidth(cnfDescArticle, 250);
	lignesWidget->setItemDelegateForColumn(cnfDescArticle, delegate);
}
//------------------------------------------------------------------------------
void CFacturesFrm::loadInfoClient(QString client)
{
	QSqlQuery *query;
	QString requete;
		
	requete="SELECT nom, adresse1, adresse2, adresse3, codePostal, ville, pays, ";
	requete+="modeReglement, conditionReglement ";
	requete+="FROM client WHERE code = \""+client+"\" ";
		
	if(CMySQL::getInstance()->execQuery(requete, &query))
	{
		if(query->next())
		{
			QSqlRecord rec=query->record();
			
			nomClient->setValue(rec.value("nom"));
			adresse1->setValue(rec.value("adresse1"));
			adresse2->setValue(rec.value("adresse2"));
			adresse3->setValue(rec.value("adresse3"));
			codePostal->setValue(rec.value("codePostal"));
			ville->setValue(rec.value("ville"));
			pays->setValue(rec.value("pays"));
			modeReglement->setValue(rec.value("modeReglement"));
			conditionReglement->setValue(rec.value("conditionReglement"));
		}else
		{
			nomClient->setValue("");
			adresse1->setValue("");
			adresse2->setValue("");
			adresse3->setValue("");
			codePostal->setValue("");
			ville->setValue("");
			pays->setValue("");
			modeReglement->setValue("");
			conditionReglement->setValue("");
		}
	}
	delete query;
}
//------------------------------------------------------------------------------
void CFacturesFrm::loadInfoAdresse(int numeroAdresse)
{
	QSqlQuery *query;
	QString requete;
		
	requete="SELECT nom, adresse1, adresse2, adresse3, codePostal, ville, pays ";
	requete+="FROM adressesClient WHERE client = \""+codeClient->getValue()+"\" AND numero = "+QString::number(numeroAdresse);
		
	if(CMySQL::getInstance()->execQuery(requete, &query))
	{
		if(query->next())
		{
			QSqlRecord rec=query->record();
			
			nomClient->setValue(rec.value("nom"));
			adresse1->setValue(rec.value("adresse1"));
			adresse2->setValue(rec.value("adresse2"));
			adresse3->setValue(rec.value("adresse3"));
			codePostal->setValue(rec.value("codePostal"));
			ville->setValue(rec.value("ville"));
			pays->setValue(rec.value("pays"));
			
		}else
		{
			nomClient->setValue("");
			adresse1->setValue("");
			adresse2->setValue("");
			adresse3->setValue("");
			codePostal->setValue("");
			ville->setValue("");
			pays->setValue("");
		}
	}
	delete query;
}
//------------------------------------------------------------------------------
void CFacturesFrm::loadGrille(void)
{
	CGlobal::EAction oldAction=getActionCur();
	QString requete;
	QSqlQuery *query;
	
	setActionCur(CGlobal::acAfficheLigne);
	
	requete="SELECT tb.nomBL, lf.numeroLigne, lf.article, lf.descriptionArticle, lf.quantite, lf.unite, ";
	requete+="lf.prixUnitaireHT, lf.quantite*lf.prixUnitaireHT, lf.tauxTva*100 AS tauxTva, 0 ";
	requete+="FROM ligneFactures AS lf ";
	requete+="LEFT JOIN ligneBLs AS lb ON lb.bl = lf.bl AND lb.numeroLigne = lf.numeroLigneBl ";
	requete+="LEFT JOIN teteBLs AS tb ON tb.code=lb.bl ";
	requete+="WHERE lf.facture="+curCode+" ";
	requete+="ORDER BY lf.bl, lf.numeroLigneBl, lf.numeroLigne";
	
	if(CMySQL::getInstance()->execQuery(requete, &query))
	{
		lignesWidget->loadGrille(query);
	}
	
	delete query;
	
	calculMontant();
	
	setActionCur(oldAction);
}
//------------------------------------------------------------------------------
void CFacturesFrm::calculMontant(int ignoreRow)
{
	int y;
	double mnt=0;
	double mntTTc=0;
	
	for(y=0;y<lignesWidget->rowCount();y++)
	{
		if(y != ignoreRow)
		{
			double mntL=lignesWidget->item(y, cnfTotal)->text().toDouble();
			mnt+=mntL;
			mntTTc+=mntL*(1+lignesWidget->item(y, cnfTva)->text().toDouble()/100);
		}
	}
	
	montant->setText(QString("%1").arg(mnt, 0, 'f', 2));
	TTC->setText(QString("%1").arg(mntTTc, 0, 'f', 2));
}
//------------------------------------------------------------------------------
void CFacturesFrm::calculTotalLigne(int row)
{
	double total=lignesWidget->item(row, cnfQuantite)->text().toDouble()*lignesWidget->item(row, cnfPrix)->text().toDouble();
	
	lignesWidget->item(row, cnfTotal)->setText(QString("%1").arg(total, 0, 'f', 2));
}
//------------------------------------------------------------------------------
bool CFacturesFrm::loadEnteteFromDevis(int numeroDevis) {
	QString requete;
	QSqlQuery *query;
	bool ret=false;
	
	requete="SELECT code, codeClient, nomClient, adresse1, adresse2, adresse3, codePostal, ville, pays, ";
	requete+="modeReglement, conditionReglement, nomDevis ";
	requete+="FROM teteDevis WHERE code = "+QString::number(numeroDevis);
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		if(query->next()) {
			QSqlRecord rec=query->record();
			
			
			if(rec.value("codeClient") == "") {
				QMessageBox msgBox(QMessageBox::Warning, tr("Erreur"), tr("Ce devis n'a pas été établie à un client"), QMessageBox::Ok);
				msgBox.exec();
				return false;
			}
			
			codeClient->setValue(rec.value("codeClient"));
			nomClient->setValue(rec.value("nomClient"));
			adresse1->setValue(rec.value("adresse1"));
			adresse2->setValue(rec.value("adresse2"));
			adresse3->setValue(rec.value("adresse3"));
			codePostal->setValue(rec.value("codePostal"));
			ville->setValue(rec.value("ville"));
			pays->setValue(rec.value("pays"));
			modeReglement->setValue(rec.value("modeReglement"));
			conditionReglement->setValue(rec.value("conditionReglement"));
			
			devis->setValue(rec.value("nomDevis"));
			devis->setData(rec.value("code"));
			commande->setValue("");
			commande->setData(QVariant());
			BLs->setValue("");
			
			ret=true;
		}
	}
	delete query;
	
	return ret;
}
//------------------------------------------------------------------------------
bool CFacturesFrm::loadEnteteFromCommande(int numeroCommande) {
	QString requete;
	QSqlQuery *query;
	bool ret=false;
	
	requete="SELECT tc.codeClient, tc.nomClient, tc.adresseFacturation1, tc.adresseFacturation2, ";
	requete+="tc.adresseFacturation3, tc.codePostalFacturation, tc.villeFacturation, tc.paysFacturation, ";
	requete+="tc.modeReglement, tc.conditionReglement, tc.devis, td.nomDevis, tc.nomCommande ";
	requete+="FROM teteCommandes AS tc ";
	requete+="LEFT JOIN teteDevis AS td ON td.code=tc.devis ";
	requete+="WHERE tc.code = "+QString::number(numeroCommande);
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		if(query->next()) {
			QSqlRecord rec=query->record();
			
			codeClient->setValue(rec.value("codeClient"));
			nomClient->setValue(rec.value("nomClient"));
			adresse1->setValue(rec.value("adresseFacturation1"));
			adresse2->setValue(rec.value("adresseFacturation2"));
			adresse3->setValue(rec.value("adresseFacturation3"));
			codePostal->setValue(rec.value("codePostalFacturation"));
			ville->setValue(rec.value("villeFacturation"));
			pays->setValue(rec.value("paysFacturation"));
			modeReglement->setValue(rec.value("modeReglement"));
			conditionReglement->setValue(rec.value("conditionReglement"));
			
			devis->setValue(rec.value("nomDevis"));
			devis->setData(rec.value("devis"));
			
			commande->setValue(rec.value("nomCommande"));
			commande->setData(numeroCommande);
			
			BLs->setValue("");
			BLs->setData(QVariant());
			
			ret=true;
		}
	}
	delete query;
	
	return ret;
}
//------------------------------------------------------------------------------
bool CFacturesFrm::loadEnteteFromClient(QString client)
{
	QString requete;
	QSqlQuery *query;
	bool ret=true;
	
	requete="SELECT tb.codeClient, tb.nomClient, tb.adresseFacturation1, tb.adresseFacturation2, ";
	requete+="tb.adresseFacturation3, tb.codePostalFacturation, tb.villeFacturation, tb.paysFacturation, ";
	requete+="tb.modeReglement, tb.conditionReglement ";
	requete+="FROM teteBLs AS tb ";
	requete+="INNER JOIN ligneBLs AS lb on lb.bl=tb.code ";
	requete+="LEFT JOIN ligneFactures AS lf ON lf.bl = lb.bl AND lf.numeroLigneBl = lb.numeroLigne ";
	requete+="WHERE tb.codeClient = \""+client+"\" AND lf.bl IS NULL AND lf.numeroLigneBl IS NULL";
	
	if(CMySQL::getInstance()->execQuery(requete, &query))
	{
		if(query->next())
		{
			QSqlRecord rec=query->record();
			
			codeClient->setValue(rec.value("codeClient"));
			nomClient->setValue(rec.value("nomClient"));
			adresse1->setValue(rec.value("adresseFacturation1"));
			adresse2->setValue(rec.value("adresseFacturation2"));
			adresse3->setValue(rec.value("adresseFacturation3"));
			codePostal->setValue(rec.value("codePostalFacturation"));
			ville->setValue(rec.value("villeFacturation"));
			pays->setValue(rec.value("paysFacturation"));
			modeReglement->setValue(rec.value("modeReglement"));
			conditionReglement->setValue(rec.value("conditionReglement"));
			
			devis->setValue("");
			devis->setData(QVariant());
			
			commande->setValue("");
			commande->setData(QVariant());
			
			BLs->setValue("");
			BLs->setData(QVariant());
			
			ret=true;
		}else
		{
			QMessageBox msgBox(QMessageBox::Warning, tr("Attention"), tr("Ce client n'a pas de BL non facturé, impossible de créer la facture !"), QMessageBox::Ok);
			msgBox.exec();
					
			ret=false;
		}
	}
	delete query;
	
	return ret;
}
//------------------------------------------------------------------------------
void CFacturesFrm::loadLigneFromDevis(int numeroDevis)
{
	QString requete="INSERT INTO ligneFactures ";
	requete+="(facture, article, descriptionArticle, quantite, prixUnitaireHT, unite, tauxTva, nomTva) ";
	requete+="SELECT "+curCode+", ld.article, ld.descriptionArticle, ld.quantite, ld.prixUnitaireHT, ";
	requete+="ld.unite, tvc.taux, tvc.nom ";
	requete+="FROM ligneDevis AS ld ";
	requete+="INNER JOIN teteFactures AS tf ON tf.code="+curCode+" ";
	requete+="INNER JOIN article AS a ON a.code=ld.article ";
	requete+="INNER JOIN tvaClient AS tvc ON tvc.codeClient = tf.codeClient AND tvc.idTva=a.idTva  ";
	requete+="WHERE ld.devis = "+QString::number(numeroDevis)+" ";
			
	if(CMySQL::getInstance()->execQuery(requete))
	{
		requete="UPDATE teteDevis SET status=\"ABO\" WHERE code="+QString::number(numeroDevis);
		CMySQL::getInstance()->execQuery(requete);
		
		loadGrille();
	}
}
//------------------------------------------------------------------------------
void CFacturesFrm::loadLigneFromCommande(int numeroCommande) {
	QString requete="INSERT INTO ligneFactures ";
	requete+="(facture, article, descriptionArticle, quantite, prixUnitaireHT, unite, tauxTva, nomTva) ";
	requete+="SELECT "+curCode+", lc.article, lc.descriptionArticle, lc.quantite, lc.prixUnitaireHT, ";
	requete+="lc.unite, tvc.taux, tvc.nom ";
	requete+="FROM ligneCommandes AS lc ";
	requete+="INNER JOIN teteFactures AS tf ON tf.code="+curCode+" ";
	requete+="INNER JOIN article AS a ON a.code=lc.article ";
	requete+="INNER JOIN tvaClient AS tvc ON tvc.codeClient = tf.codeClient AND tvc.idTva=a.idTva  ";
	requete+="WHERE lc.commande = "+QString::number(numeroCommande)+" ";
			
	if(CMySQL::getInstance()->execQuery(requete)) {
		loadGrille();
	}
}
//------------------------------------------------------------------------------
void CFacturesFrm::loadLigneFromClient(QString client) {
	QString requete="INSERT INTO ligneFactures ";
	requete+="(facture, article, descriptionArticle, quantite, prixUnitaireHT, unite, tauxTva, nomTva, bl, numeroLigneBl) ";
	requete+="SELECT "+curCode+", lb.article, lb.descriptionArticle, lb.quantite, lb.prixUnitaireHT, ";
	requete+="lb.unite, lb.tauxTva, lb.nomTva, lb.bl, lb.numeroLigne ";
	requete+="FROM ligneBLs AS lb ";
	requete+="INNER JOIN teteBLs AS tb ON tb.code=lb.bl ";
	requete+="LEFT JOIN ligneFactures AS lf ON lf.bl=lb.bl AND lf.numeroLigneBl = lb.numeroLigne ";
	requete+="WHERE tb.codeClient = \""+client+"\" AND lf.bl IS NULL AND lf.numeroLigneBl IS NULL";
	
	if(CMySQL::getInstance()->execQuery(requete)) {
		loadGrille();
	}
}
//------------------------------------------------------------------------------
bool CFacturesFrm::acceptDate(QDate dateFacturation)
{
	QString requete;
	QSqlQuery *query;
	bool ret=true;
	
	requete="SELECT COUNT(*) FROM "+masterTable+" WHERE "+primaryKey+" < \""+code->getValue()+"\" AND ";
	requete+="creation > \""+dateFacturation.toString("yyyy-MM-dd")+"\" ";
	
	if(CMySQL::getInstance()->execQuery(requete, &query))
	{
		if(query->next()) 
		{
			if(query->record().value(0).toInt() != 0)
			{
				QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
									tr("La date de la facture est inférieure à la date d'une ou plusieurs facture(s) antérieurs, êtes vous sûre de vouloir continuer ?"), QMessageBox::Yes | QMessageBox::No);
				msgBox.exec();
				
				if(msgBox.result() == QMessageBox::No)
				{
					ret=false;
				}
			}
		}
	}
	delete query;
	
	return ret;
}
//------------------------------------------------------------------------------
void CFacturesFrm::factureBLs(QString listeBl, QDate dateFacturation, int& premFact, int& derFact) {
	QString requete;
	QVariant lastCode;
	
	requete="INSERT INTO "+masterTable+" (codeClient, nomClient, adresse1, adresse2, adresse3, codePostal, ville, ";
	requete+="pays, creation, createur, modeReglement, conditionReglement, document, echeance) ";
	requete+="SELECT codeClient, nomClient, adresseFacturation1, adresseFacturation2, adresseFacturation3, ";
	requete+="codePostalFacturation, villeFacturation, paysFacturation, \""+dateFacturation.toString("yyyy-MM-dd")+"\", ";
	requete+="\""+CGlobal::getCurrentUser()+"\", modeReglement, conditionReglement, IF(document=\"L\", \"F\", \"A\"), ";
	requete+="calculEcheance(\""+dateFacturation.toString("yyyy-MM-dd")+"\", conditionReglement) ";
	requete+="FROM teteBLs WHERE code IN("+listeBl+") ";
	requete+="LIMIT 1";
	
	if(CMySQL::getInstance()->execQuery(requete, true, &lastCode)) {
		derFact=lastCode.toInt();
		
		requete="INSERT INTO ligneFactures (facture, article, descriptionArticle, quantite, ";
		requete+="prixUnitaireHT, unite, tauxTva, nomTva, bl, numeroLigneBl) ";
		requete+="SELECT "+QString::number(derFact)+", lb.article, lb.descriptionArticle, lb.quantite, ";
		requete+="lb.prixUnitaireHT, lb.unite, lb.tauxTva, lb.nomTva, lb.id ";
		requete+="FROM ligneBLs AS lb ";
		requete+="INNER JOIN teteBLs As tb ON tb.code=lb.bl ";
		requete+="LEFT JOIN ligneFactures AS lf ON lf.bl=lb.bl AND lf.numeroLigneBl = lb.numeroLigne ";
		requete+="WHERE tb.code IN("+listeBl+") AND lf.bl IS NULL AND lf.numeroLigneBl IS NULL";
		
		if(CMySQL::getInstance()->execQuery(requete)) {
			if(premFact == 0) {
				premFact=derFact;
			}
		}
	}
}
//------------------------------------------------------------------------------
void CFacturesFrm::makeRequete(QString &requete, QString &alias)
{
	requete="SELECT code, codeClient, nomClient, adresse1, adresse2, adresse3, codePostal, ";
	requete+="ville, pays, creation, modeReglement, conditionReglement, document, devis, ";
	requete+="echeance, accompte, commentaires, getBlsFromFacture(code) AS Bls, ";
	requete+="getCommandesFromFacture(code) AS commandes, getDevisFromFacture(code) AS deviss, idPont, nomFacture ";
	requete+="FROM "+masterTable;
}
//------------------------------------------------------------------------------
void CFacturesFrm::showFormulaire(QSqlQuery *query, CGlobal::EAction& action)
{
	QSqlRecord rec=query->record();
	
	curCode=rec.value("code").toString();
	
	code->setValue(curCode);
	nomFacture->setValue(rec.value("nomFacture"));
	codeClient->setValue(rec.value("codeClient"));
	nomClient->setValue(rec.value("nomClient"));
	nomClient2->setValue(!actLock->isChecked() ? rec.value("nomClient") : "");
	adresse1->setValue(rec.value("adresse1"));
	adresse2->setValue(rec.value("adresse2"));
	adresse3->setValue(rec.value("adresse3"));
	codePostal->setValue(rec.value("codePostal"));
	ville->setValue(rec.value("ville"));
	pays->setValue(rec.value("pays"));
	creation->setValue(rec.value("creation"));
	modeReglement->setValue(rec.value("modeReglement"));
	conditionReglement->setValue(rec.value("conditionReglement"));
	document->setCurrentIndex(document->findData(rec.value("document")));
	
	echeance->setValue(rec.value("echeance"));
	accompte->setValue(rec.value("accompte"));
	commentaires->setValue(rec.value("commentaires").toString());
	
	devis->setValue(rec.value("deviss"));
	devis->setData(QVariant());
		
	commande->setValue(rec.value("commandes"));
	commande->setData(QVariant());
	
	BLs->setValue(rec.value("Bls"));
	BLs->setData(QVariant());
	
	tabWidget->setTabIcon(tabWidget->count()-1, commentaires->getValue() != ""  ? QIcon(":coMexpert/images/irkickflash.png") : QIcon());
	
	if(!rec.value("idPont").isNull())
	{
		action=CGlobal::acBloque;
	}
	
	loadGrille();
}
//------------------------------------------------------------------------------
void CFacturesFrm::printMultiPage(int from, int to)
{
	QString requete="SELECT f.nomFacture AS fFrom, t.nomFacture AS fTo ";
	requete+="FROM teteFactures AS f, teteFactures AS t ";
	requete+="WHERE f.code="+QString::number(from)+" AND t.code="+QString::number(to);
	QSqlQuery *query;
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		if(query->next()) {
			QSqlRecord rec=query->record();
			QString docName=rec.value("fFrom").toString()+"_"+rec.value("fTo").toString();
			
			CRequest request;
			CReportRequestData reportRequestData;
			
			reportRequestData.setReportName("facture");
			reportRequestData.setRender("PDF");
			reportRequestData.setDocName(docName);
			
			reportRequestData.addReportParam("fromNumeroFacture", from);
			reportRequestData.addReportParam("toNumeroFacture", to);
			
			request.setToken(CGlobal::getToken());
			request.setData(&reportRequestData);
			
			CPrintFrm::getInstance()->print(&request, this);
		}
	}
	
	delete query;
}
//------------------------------------------------------------------------------
void CFacturesFrm::showChampsAuto(void) {
	QString requete="SELECT nomFacture FROM "+masterTable+" WHERE "+primaryKey+" = "+curCode;
	QSqlQuery *query;

	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		if(query->next()) {
			QSqlRecord rec=query->record();
			
			nomFacture->setValue(rec.value("nomFacture"));
		}
	}
	
	delete query;
}
//------------------------------------------------------------------------------
void CFacturesFrm::on_actAjouter_triggered(void) {
	QString client;
	CSelectTypeFactureFrm *selectTypeFactureFrm=new CSelectTypeFactureFrm(this);
	
	selectTypeFactureFrm->exec();
	
	if(selectTypeFactureFrm->result() == QDialog::Accepted) {
		client=selectTypeFactureFrm->getClient();
		
		if(selectTypeFactureFrm->isDevisSelected()) {
			int numDevis=selectTypeFactureFrm->getInfoDevis().value("teteDevis.code").toInt();
			
			setActionCur(CGlobal::acAjoutFromDevis);
			loadEnteteFromDevis(numDevis);
		}else {
			if(selectTypeFactureFrm->isCommandeSelected()) {
				int numCommande=selectTypeFactureFrm->getInfoCommande().value("teteCommandes.code").toInt();
				
				setActionCur(CGlobal::acAjoutFromCommande);
				loadEnteteFromCommande(numCommande);
			}else {	
				if(client != "") {
					setActionCur(CGlobal::acAjoutFromClient);
					if(!loadEnteteFromClient(client)) {
						chargeDonnees(curCode, CGlobal::seNone);
						return;
					}
				}else {
					setActionCur(CGlobal::acAjout);
				}
			}
		}
		
		creation->setValue(CMySQL::getInstance()->getCurrentDate());
		montant->setText("0.00");
		TTC->setText("0.00");
		tabWidget->setTabIcon(tabWidget->count()-1, QIcon());
	}
}
//------------------------------------------------------------------------------
void CFacturesFrm::on_actValider_triggered(void) {
	QString requete;
	QString condition;
	QSqlQuery *query;
	QString dateFacture;
	QString documentType;
	QVariant lastCode;
	
	calculChamps(getActionCur());
	
	if(!validChamps()) return;
	if(!validLiaison()) return;
	
	if(!QDate::fromString(creation->getValue(), "dd/MM/yyyy").isValid()) {
		QMessageBox msgBox(QMessageBox::Warning, tr("Erreur"), tr("Date incorrect"), QMessageBox::Ok);
		msgBox.exec();
		
		return;	
	}
	
	if(!acceptDate(QDate::fromString(creation->getValue(), "dd/MM/yyyy"))) {
		return;
	}
	
	dateFacture=QDate::fromString(creation->getValue(), "dd/MM/yyyy").toString("yyyy-MM-dd");
	documentType=document->itemData(document->currentIndex()).toString();
	
	requete="SELECT calculEcheance(\""+QDate::fromString(creation->getValue(), "dd/MM/yyyy").toString("yyyy-MM-dd")+"\", ";
	requete+="\""+conditionReglement->getValue()+"\")";
	
	if(!CMySQL::getInstance()->execQuery(requete, &query)) {
		delete query;
		return;
	}
	if(query->next()) {
		echeance->setValue(query->record().value(0));
	}
	delete query;
	
	switch(getActionCur()) {
		case CGlobal::acAjoutFromDevis:
			requete="SELECT COUNT(*) FROM client AS c INNER JOIN teteDevis AS td ON c.code=td.codeClient ";
			requete+="WHERE td.nomDevis='"+devis->text()+"' ";
			
			if(!CMySQL::getInstance()->execQuery(requete, &query)) {
				delete query;
				return;
			}
			if(query->next()) {
				if(query->record().value(0).toInt() == 0) {
					QMessageBox msgBox(QMessageBox::Warning, tr("Erreur"), tr("Client incorect pour le devis ")+devis->text(), QMessageBox::Ok);
					msgBox.exec();
					
					delete query;
					
					return;
				}
			}
			delete query;
		case CGlobal::acAjout:
		case CGlobal::acAjoutFromCommande:
		case CGlobal::acAjoutFromClient:
			requete="INSERT INTO "+masterTable+" SET ";
			requete+="createur=\""+CGlobal::getCurrentUser()+"\", ";
			if(devis->getData().isValid() && devis->getData().toInt() != 0) {
				requete+="devis = "+devis->getData().toString()+", ";
			}
			if(commande->getData().isValid() && commande->getData().toInt() != 0) {
				requete+="commande = "+commande->getData().toString()+", ";
			}
			condition="";
			break;
		case CGlobal::acModif:
			requete="UPDATE "+masterTable+" SET ";
			requete+="modification=CURDATE(), modificateur=\""+CGlobal::getCurrentUser()+"\", ";
			condition="WHERE "+primaryKey+"=\""+code->getValue()+"\"";
			break;
		default:
			return;
	}
	
	requete+="codeClient = \""+codeClient->getValue()+"\", nomClient = \""+nomClient->getValue()+"\", ";
	requete+="adresse1 = \""+adresse1->getValue()+"\", adresse2 = \""+adresse2->getValue()+"\", ";
	requete+="adresse3 = \""+adresse3->getValue()+"\", codePostal = \""+codePostal->getValue()+"\", ";
	requete+="ville = \""+ville->getValue()+"\", pays = \""+pays->getValue()+"\", ";
	requete+="modeReglement = \""+modeReglement->getValue()+"\", ";
	requete+="conditionReglement = \""+conditionReglement->getValue()+"\", ";
	requete+="document = \""+documentType+"\", ";
	requete+="creation = \""+dateFacture+"\", ";
	requete+="echeance = \""+QDate::fromString(echeance->getValue(), "dd/MM/yyyy").toString("yyyy-MM-dd")+"\", ";
	requete+="accompte = "+QString::number(accompte->getValue().toDouble())+", ";
	requete+="commentaires = \""+commentaires->getValue()+"\" ";
	
	requete+=condition;
	
	if(CMySQL::getInstance()->execQuery(requete, true, &lastCode) == 0) {
		return;
	}
	
	switch(getActionCur()) {
		case CGlobal::acAjoutFromDevis:
			setActionCur(CGlobal::acNone);
			curCode=lastCode.toString();
			loadLigneFromDevis(devis->getData().toInt());
			break;
		case CGlobal::acAjoutFromCommande:
			setActionCur(CGlobal::acNone);
			curCode=lastCode.toString();
			loadLigneFromCommande(commande->getData().toInt());
			break;
		case CGlobal::acAjoutFromClient:
			setActionCur(CGlobal::acNone);
			curCode=lastCode.toString();
			loadLigneFromClient(codeClient->getValue());
			break;
		case CGlobal::acAjout:
			setActionCur(CGlobal::acNone);
			curCode=lastCode.toString();
			break;
		default:
			setActionCur(CGlobal::acNone);
			curCode=code->getValue();
	}
	
	tabWidget->setTabIcon(tabWidget->count()-1, commentaires->getValue() != ""  ? QIcon(":coMexpert/images/irkickflash.png") : QIcon());
	showChampsAuto();
}
//------------------------------------------------------------------------------
void CFacturesFrm::on_actAnnuler_triggered(void)
{
	
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir annuler ?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes)
	{
		if(!chargeDonnees(curCode, CGlobal::seNone))
		{
			if(!chargeDonnees(curCode, CGlobal::sePrecedent))
			{
				if(!chargeDonnees(curCode, CGlobal::seSuivant))
				{
					setActionCur(CGlobal::acTableVide);
				}
			}
		}
	}
}
//------------------------------------------------------------------------------
void CFacturesFrm::on_actSupprimer_triggered(void) {
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir supprimer la fiche courante ?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes) {
		QString requete="DELETE FROM "+masterTable+" WHERE "+primaryKey+"="+curCode;
		CMySQL::getInstance()->execQuery(requete);
		
		if(!chargeDonnees(curCode, CGlobal::sePrecedent)) {
			if(!chargeDonnees(curCode, CGlobal::seSuivant)) {
				setActionCur(CGlobal::acTableVide);
			}
		}
	}
}
//------------------------------------------------------------------------------
void CFacturesFrm::onChange(void)
{
	if(getActionCur() == CGlobal::acNone)
	{
		setActionCur(CGlobal::acModif);
	}
	calculMontant();
}
//------------------------------------------------------------------------------
void CFacturesFrm::onChangeClient(void)
{
	if(getActionCur() == CGlobal::acNone || getActionCur() == CGlobal::acModif)
	{
		if(!avertModifClientShown) {
			QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
								tr("Etes vous sûre de modifier le client de la facture ?"), QMessageBox::Yes | QMessageBox::No);
								
			msgBox.exec();
			
			if(msgBox.result() == QMessageBox::No)
			{
				QSqlQuery *query;
				QString requete="SELECT codeClient FROM teteFactures WHERE code="+curCode;
				
				if(CMySQL::getInstance()->execQuery(requete, &query))
				{
					if(query->next())
					{
						QSqlRecord rec=query->record();
						
						codeClient->setValue(rec.value("codeClient"));
					}
				}
				
				delete query;
			}else {
				avertModifClientShown=true;
				setActionCur(CGlobal::acModif);
			}
		}else {
			setActionCur(CGlobal::acModif);
		}
	}
}
//------------------------------------------------------------------------------
void CFacturesFrm::onCombo(CMyComboBox *comboBox)
{
	CComboResultFrm *comboResultFrm=new CComboResultFrm(comboBox->getTableRemp(), comboBox->getChampRemp(), this);
	
	comboResultFrm->setModal(true);
	comboResultFrm->exec();
	
	if(comboResultFrm->result() == QDialog::Accepted)
	{
		comboBox->setValue(comboResultFrm->getResult());
		onChange();
	}
	
	delete comboResultFrm;
}
//------------------------------------------------------------------------------
void CFacturesFrm::onComboClient(CMyComboBox *comboBox)
{
	CComboResultFrm *comboResultFrm=new CComboResultFrm(comboBox->getTableRemp(), comboBox->getChampRemp(), this);
	
	comboResultFrm->setModal(true);
	comboResultFrm->exec();
	
	if(comboResultFrm->result() == QDialog::Accepted)
	{
		comboBox->setValue(comboResultFrm->getResult());
		
		loadInfoClient(comboResultFrm->getResult());
		onChange();
		return;
			
	}
	
	delete comboResultFrm;
}
//------------------------------------------------------------------------------
void CFacturesFrm::onClientEditingFinished(void)
{
	QString client=codeClient->getValue();
	
	if(client != "")
	{
		loadInfoClient(client);
	}
}
//------------------------------------------------------------------------------
void CFacturesFrm::on_actRechercheUneFiche_triggered(void)
{
	CFindFrm *findFrm=new CFindFrm(masterTable, this);
	
	findFrm->setModal(true);
	findFrm->exec();
	if(findFrm->result() == QDialog::Accepted)
	{
		chargeDonnees(findFrm->getResult(), CGlobal::seNone);
	}
}
//------------------------------------------------------------------------------
void CFacturesFrm::on_actBLsNonFacture_triggered(void)
{
	CBLsNonFactureFrm *bLsNonFactureFrm=new CBLsNonFactureFrm(this);
	
	bLsNonFactureFrm->setModal(true);
	if(bLsNonFactureFrm->exec() == QDialog::Accepted)
	{
		QList<int> li=bLsNonFactureFrm->getBLsAFacturer();
		if(li.size() != 0)
		{
			QDate dateFacturation=bLsNonFactureFrm->getDateFacture(), dateEcheance;
			QString requete;
			int i;
			QString listeBL="";
			QString oldGroupe;
			QSqlQuery *query;
			int premFact=0, derFact=0;
			
			if(!acceptDate(dateFacturation))
			{
				delete bLsNonFactureFrm;
				return;
			}
			
			requete="SELECT code, CONCAT(codeClient, document, modeReglement, conditionReglement, IF(factureSepare='OUI', code, 0)) AS groupe ";
			requete+="FROM teteBLs ";
			requete+="WHERE code IN(";
			
			for(i=0;i<li.size();i++)
			{
				if(listeBL != "") listeBL+=", ";
				listeBL+=QString::number(li.at(i));
			}
			requete+=listeBL;
			requete+=") ORDER BY groupe";
			
			if(CMySQL::getInstance()->execQuery(requete, &query))
			{
				listeBL="";
				oldGroupe="";
				while(query->next())
				{
					QSqlRecord rec=query->record();
					
					if(oldGroupe != rec.value("groupe").toString() && oldGroupe != "")
					{
						factureBLs(listeBL, dateFacturation, premFact, derFact);
						
						listeBL="";
					}
					
					if(listeBL != "") listeBL+=", ";
					listeBL+=rec.value("code").toString();
					
					oldGroupe=rec.value("groupe").toString();
				}
				
				if(oldGroupe != "")
				{
					factureBLs(listeBL, dateFacturation, premFact, derFact);
				}
				
				QMessageBox msgBox(	QMessageBox::Information, tr("Information"), 
									tr("Les factures n°")+QString::number(premFact)+tr(" à ")+QString::number(derFact)+tr(" ont été généré"), QMessageBox::Ok);
				msgBox.exec();
				printMultiPage(premFact, derFact);
			}
				
			delete query;
		}
	}
	
	delete bLsNonFactureFrm;
}
//------------------------------------------------------------------------------
void CFacturesFrm::ajouteLigne(bool &goToLast)
{
	CSelectArticleFrm *selectArticleFrm=new CSelectArticleFrm(this);
	
	if(selectArticleFrm->exec())
	{
		QString requete;
		QString article=selectArticleFrm->getArticle();
		
		requete="INSERT INTO ligneFactures (facture, article, descriptionArticle, quantite, prixUnitaireHT, unite, tauxTva, nomTva) ";
		requete+="SELECT "+curCode+", \""+article+"\", a.nom, 1, ";
		requete+="IF(pac.prix IS NOT NULL, pac.prix, a.prixUnitaireHT), a.unite, tc.taux, tc.nom ";
		requete+="FROM article AS a ";
		requete+="INNER JOIN tvaClient AS tc ON tc.idTva=a.idTva ";
		requete+="LEFT JOIN prixArticleClient AS pac ON a.code=pac.article AND pac.client = \""+codeClient->getValue()+"\" ";
		requete+="WHERE a.code = \""+article+"\" ";
		requete+="ORDER BY pac.numero DESC LIMIT 1 ";
		
		CMySQL::getInstance()->execQuery(requete);
		
		setActionCur(CGlobal::acNone);
		
		loadGrille();
	}
	delete selectArticleFrm;
}
//------------------------------------------------------------------------------
void CFacturesFrm::supprimeLigne(int row, bool& ok)
{	
	int numeroLigne=lignesWidget->item(lignesWidget->currentRow(), cnfLigne)->text().toInt();
	
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir supprimer la ligne numero ")+
						QString().number(numeroLigne), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes)
	{
		QString requete="DELETE from ligneFactures ";
		requete+="WHERE facture = "+curCode+" AND numeroLigne = "+QString().number(numeroLigne);
		
		CMySQL::getInstance()->execQuery(requete);
		
		ok=true;
	}
	
	calculMontant(row);
}
//------------------------------------------------------------------------------
void CFacturesFrm::modifiLigne(int row, int column)
{
	QString text;
	QString requete;
	QTableWidgetItem *item;
	
	if(getActionCur() != CGlobal::acNone) return;
	item=lignesWidget->item(row, column);
	
	if(column != cnfLigne && column != cnfTotal)
	{
		QString text=lignesWidget->getColumnModel(column)->format(item->text());
		QString textSQL=lignesWidget->getColumnModel(column)->formatToSQL(CGlobal::addSlashesQuot(item->text()));
		
		if(column == cnfTva)
		{
			textSQL=QString::number(textSQL.toDouble()/100);
		}
		
		requete="UPDATE ligneFactures ";
		requete+="SET "+lignesWidget->getColumnModel(column)->getNomChamp()+" = "+textSQL+" ";
		requete+="WHERE facture="+curCode+" AND numeroLigne="+lignesWidget->item(row, cnfLigne)->text();
		
		item->setText(text);
		
		CMySQL::getInstance()->execQuery(requete);
		
		calculTotalLigne(row);
		calculMontant();
		
		lignesWidget->resizeRowToContents(row);
	}
}
//------------------------------------------------------------------------------
void CFacturesFrm::ligneDoubleClicked(int row, int column)
{
	CComboResultFrm *comboResultFrm;
	switch(column)
	{
		case cnfArticle:
			comboResultFrm=new CComboResultFrm("article", "code", this);
		
			comboResultFrm->setModal(true);
			comboResultFrm->exec();
		
			if(comboResultFrm->result() == QDialog::Accepted)
			{
				QString requete;
				QTableWidgetItem *item=lignesWidget->item(row, column);
				QString ligne=lignesWidget->item(row, cnfLigne)->text();
				QString article=item->text();
				
				article=comboResultFrm->getResult();
				item->setText(article);
				
				requete="UPDATE ligneFactures AS lf ";
				requete+="INNER JOIN article AS a ON a.code=lf.article ";
				requete+="INNER JOIN tvaClient AS tc ON tc.idTva=a.idTva ";
				requete+="LEFT JOIN prixArticleClient AS pac ON pac.article=lf.article AND pac.client=\""+codeClient->getValue()+"\" ";
				requete+="SET lf.article=\""+article+"\", lf.descriptionArticle=a.nom, ";
				requete+="lf.prixUnitaireHT=IF(pac.prix IS NOT NULL, pac.prix, a.prixUnitaireHT), lf.unite=a.unite, lf.tauxTva=tc.taux ";
				requete+="WHERE a.code=\""+article+"\" AND lf.numeroLigne="+ligne+" AND lf.facture="+curCode;
				
				if(CMySQL::getInstance()->execQuery(requete))
				{
					loadGrille();
				}
			}
		
			delete comboResultFrm;
			break;
		case cnfUnite:
			comboResultFrm=new CComboResultFrm("unites", "code", this);
		
			comboResultFrm->setModal(true);
			comboResultFrm->exec();
		
			if(comboResultFrm->result() == QDialog::Accepted)
			{
				QTableWidgetItem *item;
		
				item=lignesWidget->item(row, column);
				
				item->setText(comboResultFrm->getResult());
			}
		
			delete comboResultFrm;
			break;
		default:
			break;
	}
}
//------------------------------------------------------------------------------
void CFacturesFrm::on_cmdAdresse_clicked(bool checked)
{
	CComboResultFrm *comboResultFrm=new CComboResultFrm("adressesClient", "nom", this, 0, "client = \""+codeClient->getValue()+"\"");
	
	comboResultFrm->setModal(true);
	comboResultFrm->exec();
	
	if(comboResultFrm->result() == QDialog::Accepted)
	{
		loadInfoAdresse(comboResultFrm->getResult().toInt());
		onChange();
	}
	
	delete comboResultFrm;
}
//------------------------------------------------------------------------------
void CFacturesFrm::onInvokeHelp(CChamp *champ)
{
	CHelp::getInstance()->startHelp("setSource qthelp://alpes-ressources.com.coMexpert-"+VERSION_LOGICIEL+"/doc/facture.html#"+champ->getTable()+"."+champ->getChamp());	
}
//------------------------------------------------------------------------------
void CFacturesFrm::on_actPrintMultipage_triggered(void)
{
	CSelectPlageFactureFrm *selectPlageFactureFrm=new CSelectPlageFactureFrm(this);
	
	if(selectPlageFactureFrm->exec() == QDialog::Accepted)
	{
		QString from=selectPlageFactureFrm->getFromFacture();
		QString to=selectPlageFactureFrm->getToFacture();
		
		printMultiPage(from.toInt(), to.toInt());
	}
	
	delete selectPlageFactureFrm;
}
//------------------------------------------------------------------------------
void CFacturesFrm::on_actLock_triggered(void) {
	eWidget->setVisible(actLock->isChecked());
	nomClient2->setValue(!actLock->isChecked() ? nomClient->getValue() : "");
}
//------------------------------------------------------------------------------

